using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Xml;

namespace Umdf.SideShow.Scf.ObjectModel
{
    public enum ContentBackgroundFitMode
    {
        [XmlEnum("s")]
        Scale,
        [XmlEnum("t")]
        Tile,
        [XmlEnum("c")]
        Center
    }

    public class ScfContent : ScfBodyElement
    {
        private uint _backgroundImageId;
        private ContentBackgroundFitMode _backgroundFitMode;
        private uint _contextMenuId;
        private List<ScfObject> _children;

        public uint BackgroundImageId
        {
            get { return _backgroundImageId; }
            set { _backgroundImageId = value; }
        }

        public ContentBackgroundFitMode BackgroundFitMode
        {
            get { return _backgroundFitMode; }
            set { _backgroundFitMode = value; }
        }

        public uint ContextMenuId
        {
            get { return _contextMenuId; }
            set { _contextMenuId = value; }
        }

        public List<ScfObject> Children
        {
            get
            {
                if (_children == null)
                {
                    _children = new List<ScfObject>();
                }
                return _children;
            }
        }

        protected internal override void DeserializeFromXml(System.Xml.XmlNode node)
        {
            base.DeserializeFromXml(node);
            _backgroundImageId = GetAttribute<uint>(node, "bg");
            _backgroundFitMode = GetAttribute<ContentBackgroundFitMode>(node, "bgfit");
            _contextMenuId = GetAttribute<uint>(node, "menuid");
            foreach (XmlNode childNode in node.ChildNodes)
            {
                ScfObject childObject = null;
                switch (childNode.LocalName)
                {
                    case "txt":
                        childObject = new ScfText();
                        break;
                    case "br":
                        childObject = new ScfBreak();
                        break;
                    case "img":
                        childObject = new ScfImage();
                        break;
                    case "btn":
                        childObject = new ScfButton();
                        break;
                }
                if (childObject != null)
                {
                    childObject.DeserializeFromXml(childNode);
                    Children.Add(childObject);
                }
            }
        }

        protected internal override void SerializeObject(System.IO.BinaryWriter writer)
        {
            base.SerializeObject(writer);
            writer.Write((int)_backgroundFitMode);
            writer.Write(_backgroundImageId);
            writer.Write(_contextMenuId);
            writer.Write(Children.Count);
            foreach (ScfObject c in Children)
            {
                writer.Write(c.GetType().FullName);
                c.SerializeObject(writer);
            }
        }

        protected internal override void DeserializeObject(System.IO.BinaryReader reader)
        {
            base.DeserializeObject(reader);
            _backgroundFitMode = (ContentBackgroundFitMode)reader.ReadInt32();
            _backgroundImageId = reader.ReadUInt32();
            _contextMenuId = reader.ReadUInt32();
            int count = reader.ReadInt32();
            for (int i = 0; i < count; i++)
            {
                string typeName = reader.ReadString();
                Type t = Type.GetType(typeName);
                ScfObject c = (ScfObject)Activator.CreateInstance(t);
                c.DeserializeObject(reader);
            }
        }
    }
}
